package androidx.fragment.app;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

/**
 * 重写dialogfragment
 * 解决 Can not perform this action after onSaveInstanceState
 */
public class BaseDialogFragment extends DialogFragment {
    View rootView;

    Context mContext;

    public View getRootView(ViewGroup container, int resId) {
        mContext = getActivity();
        rootView = LayoutInflater.from(mContext).inflate(resId, container, false);
        return rootView;
    }

    public <T extends View> T obtainView(int resId) {
        if (null == rootView) {
            return null;
        }
        return (T) rootView.findViewById(resId);
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        mDismissed = false;
        mShownByMe = true;
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        // 这里吧原来的commit()方法换成了commitAllowingStateLoss()
        ft.commitAllowingStateLoss();
    }
}